﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcProject.Models;

namespace MvcProject.Controllers
{
    public class MaterialUseRecordController : Controller
    {
        private BaseModels db = new BaseModels();
        //
        // GET: /MaterialUseRecord/

        public ActionResult Index(int projectId, int teamId)
        {
            ProjectInfoModel pim = new ProjectInfoModel();
            ProjectInfo pi = pim.GetProjectInfo(projectId);
            var records = db.FindMaterialUseRecordsByProjectIdTeamId(projectId, teamId);
            return View(records);
        }
        /// <summary>
        /// 查看班组成本
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public ActionResult TeamIndex(int projectId, int teamId)
        {
            ProjectInfoModel pim = new ProjectInfoModel();
            ProjectInfo pi = pim.GetProjectInfo(projectId);
            var records = db.FindMaterialUseRecordsByProjectIdTeamId(projectId, teamId);
            return View(records);
        }
        
        //
        // GET: /MaterialUseRecord/Details/5

        //public ActionResult Details(int projectId, int teamId)
        //{
        //    var records = db.FindMaterialUseRecordsByProjectIdTeamId(projectId, teamId);
        //    return View(records);
        //}
        public ActionResult Details(int id)
        {
            MaterialUseRecord dinner = db.GeMaterialUseRecord(id);
            return View(dinner);
        }
        //
        // GET: /MaterialUseRecord/Create

        public ActionResult Create(int projectId,int teamId)
        {
            ViewData["projectName"] = "";
            ViewData["teamName"] = "";
            ProjectInfoModel pim = new ProjectInfoModel();
            if (projectId != 0)
            {
                ProjectInfo pi = pim.GetProjectInfo(projectId);
                string projectName = pi.ProjectName;
                ViewData["projectName"] = "项目名称：" + projectName;
            }
            if (teamId != 0)
            {
                BaseModels bm = new BaseModels();
                Team team = bm.GetTeam(teamId);
                string teamName = team.TeamName;
                ViewData["teamName"] = "班组名称：" + teamName;
            }
            return View();
        }
        public ActionResult TeamCreate(int projectId, int teamId)
        {
            ViewData["teams"] = new SelectList(new BaseModels().FindAllTeamsByProjectId(projectId), "teamid", "teamname");
            ViewData["projectName"] = "";
            ViewData["teamName"] = "";
            ProjectInfoModel pim = new ProjectInfoModel();
            if (projectId != 0)
            {
                ProjectInfo pi = pim.GetProjectInfo(projectId);
                string projectName = pi.ProjectName;
                ViewData["projectName"] = "项目名称：" + projectName;
            }
            if (teamId != 0)
            {
                BaseModels bm = new BaseModels();
                Team team = bm.GetTeam(teamId);
                string teamName = team.TeamName;
                ViewData["teamName"] = "班组名称：" + teamName;
            }
            var records = db.FindMaterialUseRecordsByProjectIdTeamId(projectId, teamId);
            return View(records);
        } 
        //
        // POST: /MaterialUseRecord/Create

        [HttpPost]
        public ActionResult Create(int projectId,int teamId,MaterialUseRecord dinner)
        {
            if (ModelState.IsValid)
                try
                {
                    UpdateModel(dinner);
                    dinner.CreateDate = System.DateTime.Now;
                    dinner.CreateUserName = "ywx";
                    dinner.ProjectID = projectId;
                    dinner.TeamId = teamId;
                    dinner.Single_costs = dinner.Labor_costs + dinner.Materials_costs + dinner.Machinery_costs + dinner.Management_Fee;
                    db.Add(dinner);
                    db.Save();
                    return RedirectToAction("Index", new { projectId = dinner.ProjectID, teamId = dinner.TeamId });
                }
                catch (Exception e)
                {
                    return View(dinner);
                }
            return View(dinner);
        }
        
        //
        // GET: /MaterialUseRecord/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /MaterialUseRecord/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /MaterialUseRecord/Delete/5
 
        public ActionResult Delete(int id)
        {
            MaterialUseRecord dinner = db.GeMaterialUseRecord(id);
            if (dinner == null)
                return View("NotFound");
            else
                return View(dinner);
        }

        //
        // POST: /MaterialUseRecord/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            MaterialUseRecord dinner = db.GeMaterialUseRecord(id);
            if (dinner == null)
                return View("../ProjectInfo/NotFound");
            db.Delete(dinner);
            db.Save();
            return View("Index");
        }
    }
}
